package com.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.entity.ReturnSupplier;
import com.sun.service.ReturnSupplierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/4/3 13:37
 * @FileName: ReturnSupplierController
 * @Description: ""
 */
@RestController
@RequestMapping("/returnSupplier")
public class ReturnSupplierController {
    @Autowired
    private ReturnSupplierService returnSupplierService;


    /**
     * 分页查询退货给供应商数据
     *
     * @param pageNum
     * @param pageSize
     * @param supplierName
     * @return
     */
    @GetMapping("/queryReturnSupplierByPage")
    public Result<Page> queryReturnSupplierByPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                  @RequestParam(value = "limit", defaultValue = "10") int pageSize,
                                                  String supplierName, String drugName, String drugCode,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<ReturnSupplier> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ReturnSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(supplierName), ReturnSupplier::getSupplierName, supplierName);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(drugName), ReturnSupplier::getDrugName, drugName);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(drugCode), ReturnSupplier::getDrugCode, drugCode);
        lambdaQueryWrapper.gt(startTime != null, ReturnSupplier::getCreateTime, startTime);
        lambdaQueryWrapper.lt(endTime != null, ReturnSupplier::getCreateTime, endTime);
        returnSupplierService.page(pageInfo, lambdaQueryWrapper);

        return Result.success(pageInfo);
    }


    /**
     * 添加一个退货给供应商
     */
    @SystemCrmlog(title = "采购管理模块", type = "INSERT", description = "添加一个退货给供应商")
    @PostMapping
    public Result<String> addReturnSupplier(@RequestBody ReturnSupplier returnSupplier) {
        returnSupplier.setCreateTime(LocalDateTime.now());
        returnSupplierService.save(returnSupplier);
        return Result.success("新增成功");
    }

    /**
     * 删除一个退货给供应商
     */
    @SystemCrmlog(title = "采购管理模块", type = "DELETE", description = "删除一个退货给供应商")
    @DeleteMapping("/{id}")
    public Object deleteReturnSupplier(@PathVariable Long id) {
        returnSupplierService.removeById(id);
        return Result.success("删除成功");
    }


    /**
     * 修改一个退货给供应商
     */
    @SystemCrmlog(title = "采购管理模块", type = "UPDATE", description = "修改一个退货给供应商")
    @PutMapping
    public Object updateReturnSupplier(@RequestBody ReturnSupplier returnSupplier) {
        returnSupplierService.updateById(returnSupplier);
        return Result.success("修改成功");
    }
}
